import request from '@/utils/request.ts';
import type { RoleListResData, Role } from './role-type.ts';
import type { ResponseData } from '../type.ts';
import { PermissionListResData } from './permission-type.ts';

enum API {
  GET_ROLE_LIST = '/admin/acl/role',
  SAVE_ROLE = '/admin/acl/role/save',
  UPDATE_ROLE = '/admin/acl/role/update',
  DELETE_ROLE = '/admin/acl/role/remove',
  GET_ROLE_PERMISSION = '/admin/acl/permission/toAssign',
  ASSIGN_ROLE_PERMISSION = '/admin/acl/permission/doAssignAcl'
}

// 获取角色列表
export const reqGetRoleList = (
  page: number,
  limit: number,
  roleName?: string
) =>
  request.get<any, RoleListResData>(
    `${API.GET_ROLE_LIST}/${page}/${limit}${roleName ? `?roleName=${roleName}` : ''}`
  );

// 提交角色信息
export const reqSaveOrUpdateRole = (data: Role) => {
  const url = data.id ? API.UPDATE_ROLE : API.SAVE_ROLE;
  const method = data.id ? 'put' : 'post';
  return request[method]<any, ResponseData>(url, data);
};

// 获取角色权限列表
export const reqGetRolePermission = (roleId: number) =>
  request.get<any, PermissionListResData>(
    `${API.GET_ROLE_PERMISSION}/${roleId}`
  );

// 分配角色权限
export const reqAssignRolePermission = (
  roleId: number,
  permissionIdList: number[]
) =>
  request.post<any, ResponseData>(API.ASSIGN_ROLE_PERMISSION, {
    roleId,
    permissionIdList
  });

// 删除角色
export const reqDeleteRole = (roleId: number) =>
  request.delete<any, ResponseData>(`${API.DELETE_ROLE}/${roleId}`);
